import scrapy
from scrapy_move.items import ScrapyMoveItem


class MoveSpider(scrapy.Spider):
    name = 'move'
    allowed_domains = ['www.ygdy8.net']
    start_urls = ['https://www.ygdy8.net/html/gndy/china/index.html']

    def parse(self, response):
        a_list = response.xpath('//div[@class="co_content8"]//td[2]//a[2]')

        for a in a_list:
            name = a.xpath('./text()').extract_first()
            href = a.xpath('./@href').extract_first()
            # 详情页地址
            url = 'https://www.ygdy8.net' + href

            # 访问详情页
            yield scrapy.Request(url=url, callback=self.parse_second, meta={"name": name})

    def parse_second(self, response):
        # 注意 如果拿不到数据的情况下，一定检查你的xpath的语法是否正确
        src = response.xpath('//div[@id="Zoom"]//img/@src').extract_first()
        name = response.meta['name']

        movie = ScrapyMoveItem(src=src, name=name)

        yield movie
